package com.poetry.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.poetry.domain.dto.LoginDTO;
import com.poetry.domain.dto.UserDTO;
import com.poetry.domain.po.User;
import com.poetry.exception.R;
import com.poetry.service.IUserService;
import com.poetry.util.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController("/user")
@Tag(name = "用户模块")
public class UserController {
    @Resource
    private IUserService userService;

    @Operation(summary = "用户注册/登录")
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    @Operation(summary = "发送验证码")
    @PostMapping("/code")
    public R<String> sendCode(@RequestBody LoginDTO loginDTO) {
        return userService.sendVerificationCode(loginDTO);
    }

    @Operation(summary = "重置密码")
    @PostMapping("/reset_password")
    public R<String> resetPassword(@RequestBody LoginDTO loginDTO) {
        return userService.resetPassword(loginDTO);
    }

    @Operation(summary = "设置用户名或用户头像")
    @PostMapping("/set_message")
    public R<User> updateMessage(@RequestBody UserDTO userDTO) {
        return userService.updateMessage(userDTO);
    }

    @Operation(summary = "获取登录用户信息")
    @GetMapping("/get_userinfo")
    public R<UserDTO> getUserInfo() {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userService.getById(UserContext.getUserId()), userDTO);
        return R.success("获取用户信息成功", userDTO);
    }

    @Operation(summary = "注销用户")
    @DeleteMapping("/delete_user")
    public R<String> deleteUser() {
        userService.removeById(UserContext.getUserId());
        return R.success(null, "注销用户成功");
    }

    @Operation(summary = "文件上传")
    @PostMapping("/user_upload")
    public R<String> uploadFile(MultipartFile file) {
        return userService.uploadFile(file);
    }

    @Operation(summary = "用户签到")
    @PostMapping("/sign")
    public R<String> sign(){
        return userService.sign();
    }

    @Operation(summary = "用户签到天数")
    @GetMapping("/sign/count")
    public R<Integer> signCount(){
        return userService.signCount();
    }
}
